
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GraphicsEnvironment;
import java.lang.*;
import java.util.*;

import javax.swing.JFrame;

import processing.core.*;
import processing.opengl.*;

public class Arduino extends PApplet {
 
	private static final long serialVersionUID = -7799500422605536597L;
	public static ArduinoRead portReader;
	public static Road road;
	public static int startPlay = 0;
	public static int paddleX = 0;
	public static int maxPaddleX = 128;
	public static float scale = 100.0f;
	public static float speed = 40.0f;
	public static double x = 0;
	public static double y = -1.0 * scale;
	public static double z = 0;
	public static double theta = 0;
	public static boolean pauseGame = false;
	public PFont f = createFont("Arial",16,true);
	
	public void refreshPaddleX(int newPaddleX) {
		pauseGame = false;
		if(newPaddleX < paddleX + 1 || newPaddleX > paddleX + 1) {
			paddleX = newPaddleX;
			theta = Math.PI / 2.0 * paddleX / maxPaddleX - Math.PI / 4.0;
		}
	}
	
	 public static void main(String args[]) {
		 PApplet.main(new String[] { "--present", "Arduino" });
	 }
	 
	 public void setup() {
	   size(1600,800,OPENGL);
	   background(0);
	   portReader = new ArduinoRead(this);
	   road = new Road(this);
	   x = 0;
	 }

	  public void draw() {
		  if(pauseGame) return;
		  background(0);
		  float eyeX = (float) x;
		  float eyeY = (float) y;
		  float eyeZ = (float) z;
		  z += Math.cos(theta) * speed;
		  x += Math.sin(theta) * speed;
		  float cameraX = (float) x;
		  float cameraY = eyeY;
		  float cameraZ = (float) z;
		  camera(eyeX, eyeY, eyeZ, cameraX, cameraY, cameraZ, 0.0f, 1.0f, 0.0f);
		  for(Drawable marker: road.getMarkers(x, y, z)) {
			  marker.display(this);
		  }
		  //drawText("PaddleX: " + paddleX);  // STEP 6 Display Text
	  }
	  
	  public void drawText(String s) {
		  fill(255);                        // STEP 5 Specify font color
		  pushMatrix();
		  translate((float) (x + scale * Math.sin(theta)), (float) y, (float) (z + scale * Math.cos(theta)));
		  rotateY((float) (Math.PI + theta));
		  text(s, (float) (scale / -2.0), (float) (scale / -2.0), 0.0f);  // STEP 6 Display Text
		  popMatrix();
	  }
	  
	  public void gameOver() {
		  drawText("Game Over");
	      //road = new Road(this);
	      //x = 0;
	  }
	  
	  public void drawScore() {
		  drawText("Score = " + x);
	  }
	  
}
